package com.lihaozhe.commons.jsonp;

import com.lihaozhe.commons.servlet.BaseServlet;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author 李昊哲
 * @Description
 * @create 2021/4/28 下午5:38
 */
public abstract class BaseJsonpUtil {
    /**
     * 响应页面字符串做jsonp处理
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @param text     响应页面字符串
     * @throws IOException IOException
     */
    public static void print(HttpServletRequest request, HttpServletResponse response, String text) throws IOException {
        String callback = request.getParameter("callback");
        PrintWriter writer = response.getWriter();
        callback += "(" + text + ")";
        writer.println(callback);
        writer.flush();
        writer.close();
    }

    /**
     * 响应页面字符串做jsonp处理
     *
     * @param request  HttpServletRequest
     * @param response HttpServletResponse
     * @param json     响应页面对象
     * @throws IOException IOException
     */
    public static void print(HttpServletRequest request, HttpServletResponse response, Object json) throws IOException {
        BaseServlet.printJsonObject(response, json);
    }
}
